package com.ph.demo.ThreadPool;

/**
 * Created by peng on 2019/4/15.
 */

import java.util.concurrent.*;

/**
 * 拒绝策略
 */
public class RejectDemo implements Runnable{


    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<>(), Executors.defaultThreadFactory(), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                System.out.println("拒绝策略执行");
            }
        });


        for (int i = 0; i <100 ; i++) {
            executor.submit(new RejectDemo());
        }

        executor.shutdown();

    }

}
